package com.kbbywyclient.ui.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.view.ViewGroup;

import com.kbbywyclient.data.entity.PagerInfo;

import java.util.HashMap;
import java.util.Map;


/**
 * Description:BaseFaragmentViewPager的基类
 * Date: 2017/4/10
 * Created by FHW
 */


public class BaseFViewpagerAdapter extends FragmentPagerAdapter {
    private PagerInfo[] mInfoList;
    private Fragment mCurFragment;
    Context context;
    private Map<Integer,Fragment> fmap=new HashMap<>();
    public BaseFViewpagerAdapter(FragmentManager fm, PagerInfo[] infoList, Context context) {
        super(fm);
        mInfoList = infoList;
        this.context=context;
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        super.setPrimaryItem(container, position, object);
        if (object instanceof Fragment) {
            mCurFragment = (Fragment) object;
        }
    }

    public Fragment getCurFragment() {
        return mCurFragment;
    }

    @Override
    public Fragment getItem(int position) {
        PagerInfo info = mInfoList[position];
        Fragment fragment= Fragment.instantiate(context, info.getClx().getName(), info.getArgs());
        fmap.put(position,fragment);
        return fragment;
    }

    @Override
    public int getCount() {
        return mInfoList.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mInfoList[position].getTitle();
    }

    @Override
    public int getItemPosition(Object object) {
        return PagerAdapter.POSITION_NONE;
    }

    public Fragment getPostionFragment(int position){
        return fmap.get(position);
    }

}